<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script src="./js/lodash.js"></script>
    <script>
      // _.countBy(collection, [iteratee=_.identity])
      console.log(_.countBy(["one", "two", "three"], "length"));

      // _.groupBy(collection, [iteratee=_.identity])
      console.log(_.groupBy(["one", "two", "three"], "length"));

      // _.invokeMap(collection, path, [args])
      console.log(_.invokeMap([123, 456], String.prototype.split, ""));

      // _.keyBy(collection, [iteratee=_.identity])
      let array = [
        { dir: "left", code: 97 },
        { dir: "right", code: 100 },
      ];

      const ret = _.keyBy(array, function (o) {
        return String.fromCharCode(o.code);
      });

      console.log(ret, _.keyBy(array, "dir"));

      // _.orderBy(collection, [iteratees=[_.identity]], [orders])
      var users = [
        { user: "fred", age: 48 },
        { user: "barney", age: 34 },
        { user: "fred", age: 40 },
        { user: "barney", age: 36 },
      ];

      // 以 `user` 升序排序 再  `age` 以降序排序。
      console.log(_.orderBy(users, ["user", "age"], ["asc", "desc"]));

      // sortBy
      console.log(
        _.sortBy(users, function (o) {
          return o.user;
        })
      );

      // _.partition(collection, [predicate=_.identity])
      var users = [
        { user: "barney", age: 36, active: false },
        { user: "fred", age: 40, active: true },
        { user: "pebbles", age: 1, active: false },
      ];

      console.log(
        _.partition(users, function (o) {
          return o.active;
        }),
        _.partition(users, { age: 1, active: false })
      );

      // _.sample(collection)
      console.log(_.sample([1, 2, 3, 4]));

      // _.sampleSize(collection, [n=1])
      console.log(_.sampleSize([1, 2, 3], 2));

      // _.shuffle(collection)
      console.log(_.shuffle([1, 2, 3, 4]));
    </script>
  </body>
</html>
